package CRUD;


import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class SwitchUtils {

	 private final static String fccxml = "D:/Siemens/Teamcenter10/tccs/fcc.xml";
	 private final static String start_TcServer1bat = "D:/Siemens/Teamcenter10/iiopservers/start_TcServer1.bat";
	 private final static String fccReal = "PLMVOLlg:4544";
	 private final static String fccTest = "10.1.7.103:4544";
	 private final static String tcdataTest = "set TC_DATA=\\\\10.1.6.13\\tcinstall\\tcdata10win102";
	 private final static String tcdataReal = "set TC_DATA=\\\\10.1.7.197\\tc10install\\tcdata10win";

	public static void readFromFile(boolean changeToTest)
			throws FileNotFoundException, IOException {
		try {
			Path fccxmlPath = Paths.get(fccxml);
			Path start_TcServer1batPath = Paths.get(start_TcServer1bat);

			List<String> fccxmlList = Files.readAllLines(fccxmlPath,
					StandardCharsets.UTF_8);
			List<String> start_TcServer1batList = Files.readAllLines(
					start_TcServer1batPath, StandardCharsets.UTF_8);
			int fccxmlListLen = fccxmlList.size();
			int start_TcServer1batListLen = start_TcServer1batList.size();

			if (changeToTest) {
				for (int i = 0; i < fccxmlListLen; i++) {
					String temp = fccxmlList.get(i);
					if (temp.contains(fccReal)) {
						temp = temp.replaceAll(fccReal, fccTest);
					}
					fccxmlList.set(i, temp);
				}
				for (int i = 0; i < start_TcServer1batListLen; i++) {
					String temp = start_TcServer1batList.get(i);
					if (temp.contains("set TC_DATA")) {
						temp =  tcdataTest;
					}
					start_TcServer1batList.set(i, temp);

				}
			} else {
				for (int i = 0; i < fccxmlListLen; i++) {
					String temp = fccxmlList.get(i);
					if (temp.contains(fccTest)) {
						temp = temp.replaceAll(fccTest, fccReal);
					}
					fccxmlList.set(i, temp);
				}
				for (int i = 0; i < start_TcServer1batListLen; i++) {

					String temp = start_TcServer1batList.get(i);
					if (temp.contains("set TC_DATA")) {
						temp =  tcdataReal;
					}
					start_TcServer1batList.set(i, temp);
				}
			}
			Files.write(fccxmlPath, fccxmlList, Charset.forName("UTF-8"),
					StandardOpenOption.CREATE, StandardOpenOption.WRITE);
			
			Files.write(start_TcServer1batPath, start_TcServer1batList,
					Charset.forName("UTF-8"), StandardOpenOption.CREATE,
					StandardOpenOption.WRITE);
		} catch (IOException e) {

			e.printStackTrace();
		}
	}
}
